/*
Copyright 2010  Murali Devi (profdevi@gmail.com)

This file is part of ComineGL Project.

ComineGL is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

ComineGL is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with ComineGL.  If not, see <http://www.gnu.org/licenses/>.

This code uses the irrlicht and irrklang libraries.  Please read
the libraries copyrights at 

	irrlicht library:  http://irrlicht.sourceforge.net
	irrklang library:  http://www.ambiera.com/irrklang

*/



//v1.7 copyright Comine.com 20101030S18
#include "MStdLib.h"
#include "MStringArray.h"


//******************************************************
//** MStringArray
//******************************************************
void MStringArray::ClearObject(void)
	{
	mNullStringsAllowed=false;
	mList=NULL;
	mCount=0;
	mMaxCapacity=0;
	}


/////////////////////////////////////////////////
bool MStringArray::UpdateString(int index,const char *string)
	{
	if(index<0 && index>=mMaxCapacity)
		{
		return false;
		}
	else if(index<mCount)
		{
		if(mList[index]!=NULL)
			{
			delete[] mList[index];
			mList[index]=NULL;
			}
		}
	else
		{  mCount=index+1;  }
	

	mList[index]=new char[MStdStrLen(string)+1];
	if(mList[index]==NULL)
		{
		return false;
		}

	MStdStrCpy(mList[index],string);
	return true;
	}


/////////////////////////////////////////////////
bool MStringArray::DeleteString(int index)
	{
	if(mList==NULL) {  return true; }
	if(index<0 || index>=mCount) {  return true; }
	if(mList[index]==NULL)  { return true; }
	delete[] mList[index];
	mList[index]=NULL;
	return true;
	}


/////////////////////////////////////////////////
bool MStringArray::ResizeArray(int size)
	{
	if(size<0)
		{
		return false;
		}
	else if(size<mCount)
		{
		// Keep original array
		for(int i=size;i<mCount;++i)
			{  DeleteString(i); }

		mCount=size;  return true;
		}
	else if(size<mMaxCapacity)
		{
		mCount=size;
		return true;
		}

	//=Space requires more capacity then mMaxCapacity

	//allocate a new array
	char **newlist;
	newlist=new char *[size];
	if(newlist==NULL)
		{
		return false;
		}

	// Init New Array
	int i;
	for(i=mCount;i<size;++i) { newlist[i]=NULL; }

	// Copy array from original array
	for(i=0;i<mCount;++i)
		{
		newlist[i]=mList[i];
		}
	
	if(mList!=NULL)
		{
		delete[] mList;
		}

	mList=newlist;
	mMaxCapacity=mCount=size;
	return true;
	}


/////////////////////////////////////////////////
MStringArray::MStringArray(bool create,bool nullstrings)
	{
	ClearObject();
	if(create==true && Create(nullstrings)==false)
		{
		return;
		}
	}


/////////////////////////////////////////////////
MStringArray::~MStringArray(void)
	{  Destroy();  }


/////////////////////////////////////////////////
bool MStringArray::Create(bool nullstrings)
	{
	Destroy();
	mNullStringsAllowed=nullstrings;
	return true;
	}


/////////////////////////////////////////////////
bool MStringArray::Destroy(void)
	{
	Clear();
	ClearObject();
	return true;
	}


/////////////////////////////////////////////////
bool MStringArray::Clear(void)
	{
	for(int i=0;i<mCount;++i)
		{  DeleteString(i);  }

	if(mList!=NULL)
		{
		delete[] mList;
		}

	mList=NULL;
	mCount=0;
	mMaxCapacity=0;

	return true;
	}


/////////////////////////////////////////////////
int MStringArray::GetCount(void)
	{
	return mCount;
	}


/////////////////////////////////////////////////
const char *MStringArray::GetString(int index)
	{
	if(index<0 || index>=mCount)
		{
		if(mNullStringsAllowed==true) { return NULL; }
		return "";
		}

	
	if(mList[index]==NULL)
		{
		if(mNullStringsAllowed==true) { return NULL; }
		return "";
		}

	return mList[index];
	}


/////////////////////////////////////////////////
bool MStringArray::SetString(int index,const char *string)
	{
	if(index>=mMaxCapacity)
		{
		if(ResizeArray(index+1)==false)
			{
			return false;
			}
		}

	return UpdateString(index,string);
	}


/////////////////////////////////////////////////
bool MStringArray::SetString(const char *string)
	{
	ResizeArray(mCount+1);
	return SetString(mCount-1,string);
	}


////////////////////////////////////////////////
bool MStringArray::Remove(int index)
	{
	if(index<0 || index>=mCount)
		{
		return false;
		}

	if(DeleteString(index)==false)
		{
		return false;
		}

	// Move All Strings down by one
	int i;
	for(i=index+1;i<mCount;++i)
		{  mList[i-1]=mList[i];  }

	mList[mCount-1]=NULL;
	mCount=mCount-1;

	return true;
	}


////////////////////////////////////////////////////
bool MStringArray::IsMember(const char *string)
	{
	int i;
	for(i=0;i<mCount;++i)
		{
		if(string==NULL)
			{
			if(mList[i]==NULL) { return true; }
			continue;
			}
		
		if(MStdStrCmp(mList[i],string)==0)
			{
			return true;
			}
		}

	return false;
	}


